package com.yuyou.fn.educational.discount;

import com.github.wens.mybatisplus.examples.Example;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.educational.entity.ExternalRegDetail;
import com.yuyou.fn.educational.entity.Period;
import com.yuyou.fn.educational.entity.StudentReg;
import com.yuyou.fn.educational.service.IExternalRegDetailService;
import com.yuyou.fn.educational.service.IPeriodService;
import com.yuyou.fn.educational.service.IStudentRegService;

import java.util.Date;


public class Helper {

    private IPeriodService periodService;

    private IStudentRegService studentRegService;

    private IExternalRegDetailService externalRegDetailService;

    public Helper(IPeriodService periodService, IStudentRegService studentRegService, IExternalRegDetailService externalRegDetailService) {
        this.periodService = periodService;
        this.studentRegService = studentRegService;
        this.externalRegDetailService = externalRegDetailService;
    }

    public Long countStudentRegAmount(Long studentId, Long areaTeamId, String periodName) {
        Period period = periodService.findPeriodByAreaTeamIdPeriodName(areaTeamId, periodName);
        long l = studentRegService.countByPeriodIdStudentIdRegStatus(period.getPeriodId(), studentId, 0);
        if (l > 0) {
            return l;
        }
        Example<ExternalRegDetail> externalRegDetailExample = ExternalRegDetail.createExample();
        externalRegDetailExample.createCriteria().andEqualTo("studentId", studentId).andEqualTo("regStatus", 0).andLessThan("periodId", period.getPeriodId());
        return externalRegDetailService.countByExample(externalRegDetailExample);

    }

    public boolean isOldStudent(Long studentId, Long periodId) {

        Example<StudentReg> example = StudentReg.createExample();
        example.createCriteria().andEqualTo("studentId", studentId).andEqualTo("regStatus", 0).andEqualTo("chargingStatus" ,2 ).andLessThan("periodId", periodId);
        long l = studentRegService.countByExample(example);
        if (l > 0) {
            return true;
        }

        Example<ExternalRegDetail> externalRegDetailExample = ExternalRegDetail.createExample();
        externalRegDetailExample.createCriteria().andEqualTo("studentId", studentId).andEqualTo("regStatus", 0).andLessThan("periodId", periodId);
        return externalRegDetailService.countByExample(externalRegDetailExample) > 0;

    }

    public boolean hasDiscount(Long studentId, Long classId, Long discountId) {
        Example<StudentReg> example = StudentReg.createExample();
        example.createCriteria().andEqualTo("studentId", studentId).andEqualTo("classId", classId).andEqualTo("regStatus", 0).andEqualTo("chargingStatus" ,2 ).andLike("discountDetail", "%|" + discountId + "%");
        long l = studentRegService.countByExample(example);
        return l > 0 ? true : false;
    }

    public boolean hasReg(Long studentId, Long classId, Integer startAmount, Integer endAmount) {
        Example<StudentReg> example = StudentReg.createExample();
        example.createCriteria().andEqualTo("classId", classId).andEqualTo("studentId", studentId).andEqualTo("regStatus", 0).andEqualTo("chargingStatus" ,2 ).andEqualTo("startAmount", startAmount).andEqualTo("endAmount", endAmount);
        return studentRegService.countByExample(example) > 0 ? true : false;
    }

    public boolean hasReg(Long studentId, Long classId, Integer startAmount, Integer endAmount , String regTime ) {
        String[] times = regTime.split("~");
        Example<StudentReg> example = StudentReg.createExample();
        example.createCriteria().andEqualTo("classId", classId).andEqualTo("studentId", studentId).andEqualTo("regStatus", 0).andEqualTo("chargingStatus" ,2 ).andEqualTo("startAmount", startAmount).andEqualTo("endAmount", endAmount).andBetween("createTime" , parseDate(times[0].trim()) , parseDate(times[1].trim()) );
        return studentRegService.countByExample(example) > 0 ? true : false;
    }

    private Date parseDate(String time ){
        return TimeUtils.parse(time,"yyyy-MM-dd HH:mm:ss");
    }


}
